'use strict'

const { Controller } = require('egg')

class KujoMiva extends Controller {
  /**
  * @api {get} /KujoMiva 获取列表
  * @apiGroup KujoMiva
  */
  async index() {
      const { ctx } = this
      const { proid: pro_id } = ctx.header

      const result = await this.app.databaseWuye.select('jobs', {
          where: { pro_id },
          orders: [['id', 'desc']]
      })

      ctx.returnBody(200, '进入了获取列表接口', result)
  }

  /**
  * @api {get} /KujoMiva/:id 获取详情
  * @apiGroup KujoMiva
  */
  async show() {
    const { ctx } = this

    ctx.returnBody(200, '进入了获取详情接口')
  }

  /**
  * @api {post} /KujoMiva 创建数据
  * @apiGroup KujoMiva
  */
  async create() {
      const { ctx } = this

      const {
          occ = '新增职位',
          recNum = '0',
          claim = '新增要求',
      } = ctx.request.body
      const {
          pro_id
      } = ctx.header

      const result = await this.app.databaseWuye.insert('jobs', {
          occ,
          recNum,
          claim,
          pro_id
      })

      ctx.returnBody(200, '添加成功', {
          id: result.insertId
      })
  }

  /**
  * @api {put} /KujoMiva/:id 更改数据
  * @apiGroup KujoMiva
  */
  async update() {
      const { ctx } = this
      const { id } = ctx.params

      await this.app.databaseWuye.beginTransactionScope(async conn => {
          const oldData = await conn.get('jobs', { id })
          const {
              occ = oldData.occ,
              recNum = oldData.recNum,
              claim = oldData.claim
          } = ctx.request.body
          const updateRow = {
              occ,
              recNum,
              claim,
          }
          await conn.update('jobs', updateRow, { where: { id } })
      })
      ctx.returnBody(200, '修改成功')
  }

  /**
  * @api {delete} /KujoMiva/:id 删除数据
  * @apiGroup KujoMiva
  */
  async destroy() {
      const { ctx } = this
      const { id } = ctx.params

      const result = await this.app.databaseWuye.delete('jobs', {
          id
      })

      ctx.returnBody(200, '删除成功', result)
  }
}

module.exports = KujoMiva
